package com.ruoyi.web.controller.admin;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BdmUser;
import com.ruoyi.system.domain.dto.BdmUserTeamDto;
import com.ruoyi.system.domain.vo.BdmOrderVo;
import com.ruoyi.system.domain.vo.BdmUserTeamVo;
import com.ruoyi.system.service.IBdmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/admin/user")
public class BdmUserController extends BaseController
{
    @Autowired
    private IBdmUserService bdmUserService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BdmUser bdmUser)
    {
        startPage();
        List<BdmUser> list = bdmUserService.selectBdmUserList(bdmUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdmUser bdmUser)
    {
        List<BdmUser> list = new ArrayList<>();
        ExcelUtil<BdmUser> util = new ExcelUtil<BdmUser>(BdmUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bdmUserService.selectBdmUserById(id));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@RequestBody BdmUser bdmUser)
    {
        return toAjax(bdmUserService.insertBdmUser(bdmUser));
    }

    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@RequestBody BdmUser bdmUser)
    {
        return toAjax(bdmUserService.updateBdmUser(bdmUser));
    }

    /**
     * 删除用户
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bdmUserService.deleteBdmUserByIds(ids));
    }

    /**
     * 团队人数列表
     * @param dto
     * @return {@link BdmUserTeamVo }
     */
    @GetMapping("/getTeamNumber")
    public TableDataInfo getTeamNumber(BdmUserTeamDto dto) {
        startPage();
        List<BdmUserTeamVo> list=bdmUserService.getTeamNumber(dto);
        return getDataTable(list);
    }

    /**
     * 团队人数列表 团队业绩
     * @param dto
     * @return {@link BdmOrderVo }
     */
    @GetMapping("/getTeamPerformance")
    public TableDataInfo getTeamPerformance(BdmUserTeamDto dto) {
        startPage();
        return getDataTable(bdmUserService.getTeamPerformance(dto));
    }

    /**
     * 团队人数列表 团队业绩统计
     * @param dto
     * @return {@link BdmOrderVo }
     */
    @GetMapping("/getTeamPerformanceStatistics")
    public AjaxResult getTeamPerformanceStatistics(BdmUserTeamDto dto) {
        BdmOrderVo vo = new BdmOrderVo();
        List<BdmOrderVo> teamPerformance = bdmUserService.getTeamPerformance(dto);
        if (!CollectionUtils.isEmpty(teamPerformance)){
            vo.setOrderNumber(teamPerformance.size());
            vo.setOrderMoney(teamPerformance.stream().map(BdmOrderVo::getPayMoney).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add));
        }
        return success(vo);
    }

    /**
     * 修改用户
     */
    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody BdmUser bdmUser)
    {
        BdmUser user = bdmUserService.lambdaQuery().eq(BdmUser::getSysUserid, bdmUser.getSysUserid()).one();
        if (user == null){
            throw new RuntimeException("该用户不存在");
        }
        bdmUser.setId(user.getId());
        return toAjax(bdmUserService.updateBdmUser(bdmUser));
    }
}
